/**
 * 功能描述
 */

define(function (require, exports, module) {
	
	var Subnav = require("subnav");
	var subnavEvents = require("../handler/subnavEvents");
	var roomTypeDataSvc = require("../store/roomTypeDataSvc");
	var user = require("store").get("user");
	
    var instance = {
        initComponent: function (params, widget) {
        	
        	var subnav = new Subnav({
        	    parentNode: ".J-subnav",
        	    model: {
        	        title: "房型定价",
        	        items: [{
        	            id: "servicepoint",
						type:"buttongroup",
						tip:"服务点",
						keyField :"pkServicePoint",
						valueField :"name",
						lazy:true,
						params:{
							fetchProperties:"pkServicePoint,name"
						},
						url:"api/servicepoint/operableservicepoints",
						items:user.privateServicePoints,
						handler:function(key,element){
							//查询楼宇
		                    roomTypeDataSvc.getOrgBuilding(key,function(buildingData){
		                    	subnav.setData("buiding", buildingData);
		                    	if(buildingData.length>0){
		                    		//查询房间类型
			                    	var pkOrgbuilding = subnav.getValue("buiding");
			                    	roomTypeDataSvc.getRoomtype(key,pkOrgbuilding,function(roomTypeData){
			                        	subnav.setData("roomtypes", roomTypeData);
			                        	subnavEvents.refreshCalendar(widget);
			                        });	
		                    	}else{
		                    		subnavEvents.refreshCalendar(widget);
		                    	}
		                    });
						}
        	        },{
        	            id: "buiding",
        	            type: "buttongroup",
        	            tip:"楼宇",
        	            keyField: "pkOrgBuilding",
        	            valueField: "name",
        	            items:[],
        	            handler:function(key,element){
        	            	//查询房间类型
        	            	var servicepoint = subnav.getValue("servicepoint");
	                    	roomTypeDataSvc.getRoomtype(servicepoint,key,function(roomTypeData){
	                        	subnav.setData("roomtypes", roomTypeData);
	                        	
	                        	console.log(roomTypeData);
	                        	subnavEvents.refreshCalendar(widget);
	                        });
						}
        	        },{
        	            id: "roomtypes",
        	            type: "buttongroup",
        	            tip:"房型",
        	            keyField: "pkRoomType",
        	            valueField: "name",
        	            items:[],
        	            handler:function(key,element){
        	            	subnavEvents.refreshCalendar(widget);
						}
        	        },{
        	            id: "add",
        	            type: "button",
        	            text: "定价",
        	            handler: function() {
        	                subnav.hide(["add","servicepoint","buiding","roomtypes"]).show(["return"]);
        	                widget.get("monthRange").hide();
        	                $(".J-calendar").hide();
        	                widget.show([".J-form"]);
        	            }
        	        }, {
        	            id: "return",
        	            type: "button",
        	            text: "返回",
        	            show: false,
        	            handler: function() {
        	                subnav.hide(["return"]).show(["add","servicepoint","buiding","roomtypes"]);
        	                widget.get("monthRange").show();
        	                $(".J-calendar").show();
        	                widget.hide([".J-form"]);
        	                widget.get("form").reset();
        	            }
        	        }]
        	    }
        	});
        	widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
